from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse, JSONResponse
from app.models.models import SysUser, SysMenu
from app.utils.auth import get_current_active_user
from app.database.database import get_db
from sqlalchemy.orm import Session

router = APIRouter()
templates = Jinja2Templates(directory="app/templates")

# 页面路由 - 返回仪表盘HTML页面
@router.get("/dashboard", response_class=HTMLResponse)
async def get_dashboard_page(request: Request):
    return templates.TemplateResponse("dashboard.html", {"request": request})

# API路由 - 返回统计数据
@router.get("/api/stats")
async def get_stats(db: Session = Depends(get_db), current_user: SysUser = Depends(get_current_active_user)):
    # 统计用户数量
    user_count = db.query(SysUser).count()
    
    # 统计激活用户数量
    active_user_count = db.query(SysUser).filter(SysUser.is_active == 1).count()
    
    # 统计菜单数量
    menu_count = db.query(SysMenu).count()
    
    # 返回统计数据
    return {
        "user_count": user_count,
        "active_user_count": active_user_count,
        "menu_count": menu_count,
        "system_count": 1,  # 当前只有一个系统
        "user_growth": [  # 示例数据：每月用户增长
            {"month": "1月", "count": 5},
            {"month": "2月", "count": 10},
            {"month": "3月", "count": 15},
            {"month": "4月", "count": 12},
            {"month": "5月", "count": 20},
            {"month": "6月", "count": user_count}
        ],
        "activity": [  # 示例数据：用户活跃度
            {"name": "非常活跃", "percentage": 30},
            {"name": "活跃", "percentage": 40},
            {"name": "一般", "percentage": 20},
            {"name": "不活跃", "percentage": 10}
        ]
    } 